/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tests.taskgraph.tasks.data;

import java.io.IOException;

import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import taskgraph.channels.Channel;
import taskgraph.ports.InputPort;
import taskgraph.ports.OutputPort;
import taskgraph.tasks.data.Merge;

public class MergeTest {

	/**
	 * Runs the test suite in this class from the command line.
	 * 
	 * @param args	Arguments are ignored.
	 */
	public static void main(String[] args) {
		org.junit.runner.JUnitCore.runClasses(MergeTest.class);
	}
	
	/**
	 * Provides compatibility with 3.x versions of JUnit.
	 * 
	 * @return A 3.x-compatible test suite.
	 */
	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(MergeTest.class);
	}
	
	private InputPort<Product> first;
	private InputPort<Product> second;
	private OutputPort<Product> output;
	
	@Before
	public void setup() {
		Channel<Product> channel = new Channel<Product>();
		output = channel.getOutputPort();
		first = channel.getInputPort();
		second = channel.getInputPort();
	}
	
	@After
	public void cleanup() throws IOException {
		output.close();
		first.close();
	}
	
    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the constructors.                                               | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void defaultConstructor() {
		Merge<Product> task = new Merge<Product>();
		Assert.assertNotNull(task);
	}
	
	@Test
	public void defaultInstanceClass() {
		Merge<Product> task = new Merge<Product>();
		Assert.assertEquals(Merge.class, task.getClass());
	}

	@Test
	public void constructorWithPorts() {
		Merge<Product> task = new Merge<Product>(first, second, output);
		Assert.assertNotNull(task);
	}
	
	@Test
	public void constructorWithPortsAndClass() {
		Merge<Product> task = new Merge<Product>(first, second, output);
		Assert.assertEquals(Merge.class, task.getClass());
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the run method.                                                 | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void testRun() throws IOException {
		
		/*
		 * This test goes line this:
		 * [@Test]----first channel--->[Merge]---output channel--->[@Test]
		 * [@Test]---second channel--->[-----]
		 */
		Product[] firstList = {
			new Product(5005, "Notebook", 2.50f),
			new Product(1400, "Pencil",   0.49f),
			new Product(0001, "Eraser",   0.60f),
			new Product(4040, "Bookend", 10.25f)
		};

		Product[] secondList = {
			new Product(1995, "Binder",   1.50f),
			new Product(6660, "Envelops", 4.00f),
			new Product(8080, "Paper",    6.50f),
			new Product(1252, "Markers",  3.95f)
		};

		Channel<Product> firstChannel = new Channel<Product>();
		Channel<Product> secondChannel = new Channel<Product>();
		Channel<Product> outputChannel = new Channel<Product>();
		
		Merge<Product> task = 
			new Merge<Product>(firstChannel.getInputPort(), 
					           secondChannel.getInputPort(),
					           outputChannel.getOutputPort());
		
		// Write lists to output port of the task's input channels.
		OutputPort<Product> firstPort = firstChannel.getOutputPort();		
		for (Product p : firstList)
			firstPort.write(p);
		firstPort.close();
		OutputPort<Product> secondPort = secondChannel.getOutputPort();		
		for (Product p : secondList)
			secondPort.write(p);
		secondPort.close();
				
		task.run();
		
		InputPort<Product> outputPort = outputChannel.getInputPort();

		// Now compare the output with the interleaved inputs.
		
		for (int i = 0, n = firstList.length; i < n; ++i) {
			Assert.assertEquals(firstList[i], outputPort.read());
			Assert.assertEquals(secondList[i], outputPort.read());
		}
		outputPort.close();
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests for constructors.                                     | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullFirstPort() throws IOException {
		new Merge<Product>(null, second, output);
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullSecondPort() throws IOException {
		new Merge<Product>(first, null, output);
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullOutputPort() throws IOException {
		new Merge<Product>(first, second, null);
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests.                                                      | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

}
